package com.dodayum.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.dodayum.dao.WeiBoNews;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class GetWeiBoNewsService {

    /**
     * 热点
     */
    private static final String HOT_URL = "https://weibo.com/ajax/side/hotSearch";

    // 我的热点
//    private static final String MY_HOT_URL = "https://weibo.com/ajax/statuses/mineBand";

    /**
     * 获取热搜
     * @return {@link WeiBoNews}
     */
    public WeiBoNews getWeiBoNews() {
        try (HttpResponse execute = HttpRequest.get(HOT_URL).execute()) {
            if (ObjectUtil.isEmpty(execute) || execute.getStatus() != 200) {
                throw new RuntimeException("请求微博接口出现错误，" + JSON.toJSONString(execute));
            }
            String responseString = execute.body();
            return JSON.parseObject(responseString, WeiBoNews.class);
        }

    }

}
